﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using ACADApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using ACADDocument = Autodesk.AutoCAD.ApplicationServices.Document;

namespace YNCNumberSys
{
    public class ZJAppendXrecord
    {
        #region 属性
        
        /// <summary>
        /// 数据字典.
        /// </summary>
        DBDictionary _dct_DBDictionary;
        /// <summary>
        /// 扩展记录名.
        /// </summary>
        string _str_XrecordName;
        /// <summary>
        /// 扩展记录链表.
        /// </summary>
        ResultBuffer _rbf_XrecordData;
        /// <summary>
        /// 事务.
        /// </summary>
        Transaction _trs_Transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase _wdb_DataBase;
        /// <summary>
        /// 该值如果为True,则(1)_trs_Transaction,_wdb_DataBase均为当前图纸空间;(2)事务提交在该类内完成.
        /// </summary>
        private bool _b_TransactionIsUsed;
        #endregion

        public ZJAppendXrecord(ACADDatabase wdb_DataBase,Transaction trs_Transaction,
            DBDictionary dct_DBDictionary, string str_XrecordName, ResultBuffer rbf_XrecordData)
        {
            _wdb_DataBase = wdb_DataBase;
            _trs_Transaction = trs_Transaction;
            _dct_DBDictionary = dct_DBDictionary;
            _str_XrecordName = str_XrecordName;
            _rbf_XrecordData = rbf_XrecordData;
            _b_TransactionIsUsed = true;
        }
        public ZJAppendXrecord(DBDictionary dct_DBDictionary, string str_XrecordName, ResultBuffer rbf_XrecordData)
        {
            _wdb_DataBase = HostApplicationServices.WorkingDatabase;
            _trs_Transaction = null;
            _dct_DBDictionary = dct_DBDictionary;
            _str_XrecordName = str_XrecordName;
            _rbf_XrecordData = rbf_XrecordData;
            _b_TransactionIsUsed = false;
        }

        private void DoAppendXrecord()
        {
            Xrecord xrec_Xrecord;
            if (_dct_DBDictionary.Contains(_str_XrecordName))
            {
                xrec_Xrecord = (Xrecord)_trs_Transaction.GetObject(_dct_DBDictionary.GetAt(_str_XrecordName), OpenMode.ForWrite, false);
            }
            else
            {
                _dct_DBDictionary.UpgradeOpen();
                xrec_Xrecord = new Xrecord();
                _dct_DBDictionary.SetAt(_str_XrecordName, xrec_Xrecord);
                _trs_Transaction.AddNewlyCreatedDBObject(xrec_Xrecord, true);
            }
            xrec_Xrecord.Data = _rbf_XrecordData;
        }
        public void Do()
        {
            if (!_b_TransactionIsUsed)
            {
                using (_trs_Transaction = _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    DoAppendXrecord();
                    _trs_Transaction.Commit();
                    _trs_Transaction.Dispose();
                }
            }
            else
            {
                DoAppendXrecord();
            }
        }
    }
}
